package cn.bone.base.boot.demo1.web;

import cn.bone.base.boot.demo1.bean.Phone;
import com.google.gson.Gson;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.math.BigDecimal;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

/**
 * Created by MD-01 on 2016/9/8.
 */
@RestController
@RequestMapping("/mongo")
public class MongoController {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private GridFsTemplate gridFsTemplate;

    @RequestMapping("/saveFileInDoc")
    public void saveFileInDoc(){

        Phone phone = new Phone();
        phone.setName("魅族");
        phone.setOs("android");
        phone.setPrice(BigDecimal.valueOf(1000));

        try {
            phone.setBytes(IOUtils.toByteArray(new FileInputStream("d:/lg.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        template.insert(phone);
    }


    //57d0d53c6815331da068f47a

    @RequestMapping("/readFileIndoc")
    public void readFileIndoc(){

        Phone phone = template.findById("57d0d53c6815331da068f47a",Phone.class,"phone");
        byte[] bytes = phone.getBytes();
        try(OutputStream os = FileUtils.openOutputStream(new File("d:/123.png"))) {
            IOUtils.write(bytes,os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @RequestMapping("/saveFileInGridFS")
    public void saveFileInGridFS(){
        try (InputStream is = new FileInputStream(("d:/lg.png"))){
            GridFSFile gfsFile = gridFsTemplate.store(is, "lg.png");
            Gson gson = new Gson();
            String str = gson.toJson(gfsFile);
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/readFileInGridFS")
    public void readFileInGridFS(){

        GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(query(where("filename").is("lg.png")));
        try {
            FileUtils.copyInputStreamToFile(gridFSDBFile.getInputStream(),new File("d:/abc.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteFile")
    public void deleteFile(){

       /* Criteria criteria = Criteria.where("filename").is("lg.png")
                .orOperator(Criteria.where("filename").is("123.png"));
        Query query = new Query(criteria);
        gridFsTemplate.delete(query);*/


        gridFsTemplate.delete(query(where("filename").is("lg.png")));
    }

}


